package com.bwei.like.mynewstop;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import android.view.ViewGroup;
>>>>>>> NewsTop
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import com.bwei.like.mynewstop.activity.BaseActivity;
>>>>>>> NewsTop
import com.bwei.like.mynewstop.adapater.MoreChnanelAdapater;
import com.bwei.like.mynewstop.adapater.MyChnanelAdapater;
import com.bwei.like.mynewstop.app.MyApplication;
import com.bwei.like.mynewstop.fragment.LocalCityFragment;
import com.bwei.like.mynewstop.fragment.MenuFragment;
import com.bwei.like.mynewstop.manager.FragmentFactory;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.utils.TitileUtils;
import com.bwei.like.mynewstop.view.LazyViewPager;

import com.bwei.like.mynewstop.view.ViewPageIndicator;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.umeng.socialize.UMShareAPI;

import org.xutils.common.util.LogUtil;


<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
public class MainActivity extends SlidingFragmentActivity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
=======
public class MainActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
>>>>>>> NewsTop

    private ViewPageIndicator viewPageIndicator;
    private SlidingMenu slidingMenu;
    private LazyViewPager lazyViewPager;
    private Button main_title_button;
    private ImageView main_title_user;
    private ProgressBar mian_title_progressBar;
    private TextView myChnanel_text;
    private TextView myCompile_text;
    private RelativeLayout myChnanel_relativeLayout;
    private ScrollView scrollView;
    private GridView gv_myChnanel;
    private GridView gv_more_Chnanel;
    private ImageButton iv_Chnanel;
    private boolean isShowChnanel = true;
    private boolean complete = true;
    private MyChnanelAdapater myChnanelAdapater;
    private MoreChnanelAdapater moreChnanelAdapater;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private LinearLayout scroll_LinearLayout;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
    private RelativeLayout main_RelativeLayout;
>>>>>>> NewsTop


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935

        initMainViewPageIndicator();
        //设置slidingMenu侧滑菜单
        initSlidingMenu();
=======
        initMainViewPageIndicator();
        //设置slidingMenu侧滑菜单
        initSlidingMenu();
        if (MyApplication.isNightMode){
            setBackGroudColor(main_RelativeLayout);
        }
>>>>>>> NewsTop

        //监听小图标弹出侧滑框
        main_title_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggle();
            }
        });

        //设置gridview适配器
        myChnanelAdapater = new MyChnanelAdapater(TitileUtils.myChannel, this);
        moreChnanelAdapater = new MoreChnanelAdapater(TitileUtils.moreChannel, this);
        gv_myChnanel.setAdapter(myChnanelAdapater);
        gv_more_Chnanel.setAdapter(moreChnanelAdapater);

    }

    private void initView() {
        lazyViewPager = (LazyViewPager) findViewById(R.id.viewpage);
        viewPageIndicator = (ViewPageIndicator) findViewById(R.id.viewpageIndictor);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
        main_RelativeLayout = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
>>>>>>> NewsTop

        main_title_button = (Button) findViewById(R.id.main_title_button);
        main_title_user = (ImageView) findViewById(R.id.main_title_image);
        mian_title_progressBar = (ProgressBar) findViewById(R.id.main_progressBar);
        iv_Chnanel = (ImageButton) findViewById(R.id.iv_Chnanel);
        scroll_LinearLayout = (LinearLayout) findViewById(R.id.scroll_LinearLayout);

        //查找我的频道的控件
        myChnanel_relativeLayout = (RelativeLayout) findViewById(R.id.myChnanel_relativeLayout);
        myChnanel_text = (TextView) findViewById(R.id.myChannel_text);
        myCompile_text = (TextView) findViewById(R.id.myCompile_text);

        scrollView = (ScrollView) findViewById(R.id.scroll);
        gv_more_Chnanel = (GridView) findViewById(R.id.gv_more_Chnanel);
        gv_myChnanel = (GridView) findViewById(R.id.gv_my_Chnanel);

        iv_Chnanel.setOnClickListener(this);
        myChnanel_text.setOnClickListener(this);
        myCompile_text.setOnClickListener(this);


        gv_myChnanel.setOnItemClickListener(this);
        gv_more_Chnanel.setOnItemClickListener(this);
        gv_myChnanel.setOnItemLongClickListener(this);

    }

    private void initSlidingMenu() {

        // 设置侧拉条目布局
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        setBehindContentView(R.layout.sliding_menu_frame);
=======

>>>>>>> NewsTop
        // 获取侧拉栏目对象
        slidingMenu = getSlidingMenu();

        /*
         * SlidingMenu.TOUCHMODE_FULLSCREEN全屏触摸有效 SlidingMenu.TOUCHMODE_MARGIN
         * 拖拽边缘有效 SlidingMenu.TOUCHMODE_NONE 不响应触摸事件
         */
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        // 设置内容显示页对应的dp大小
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // //设置左侧侧拉栏目宽度
        // slidingMenu.setBehindWidth(140);
        // 设置侧拉栏目所在位置
         /*
         * SlidingMenu.LEFT SlidingMenu.LEFT_RIGHT SlidingMenu.RIGHT
         */
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 给侧拉栏目和左侧内容页区分开(加线)
        slidingMenu.setShadowDrawable(R.mipmap.ic_launcher);
        // 设置线的宽度
        slidingMenu.setShadowWidthRes(R.dimen.shadow_width);

        // fragment去替换布局中节点
        MenuFragment menuFragment = new MenuFragment();
        // FragmentManager管理者
        getSupportFragmentManager()
                // 开启事物
                .beginTransaction()
                // 通过fragment去替换对应布局
                .replace(R.id.menu, menuFragment, "MENU")
                // 提交事物
                .commit();
    }

    private void initMainViewPageIndicator() {

        //获取导航类型数据
        String myChannel = CommonUtils.getSp("myChannel");
        String moreChannel = CommonUtils.getSp("moreChannel");

        //将读取的数据拆分开
        String[] myChannels = myChannel.split(",");
        String[] moreChannels = moreChannel.split(",");

        //将获取的数据分别保存进集合中
        for (int i = 0; i < myChannels.length; i++) {
            TitileUtils.myChannel.add(myChannels[i]);
        }

        for (int i = 0; i < moreChannels.length; i++) {
            TitileUtils.moreChannel.add(moreChannels[i]);
        }


        //viewpage适配器展示数据
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = FragmentFactory.getFragment(TitileUtils.myChannel.get(position).substring(TitileUtils.myChannel.get(position).indexOf(":") + 1));
                return fragment;
            }

            @Override
            public int getCount() {
                return TitileUtils.myChannel.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return TitileUtils.myChannel.get(position).substring(0, TitileUtils.myChannel.get(position).indexOf(":"));
            }
        };
        lazyViewPager.setAdapter(fragmentPagerAdapter);
        viewPageIndicator.setViewPage(lazyViewPager, getSlidingMenu());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        if (requestCode == LocalCityFragment.REQUESTCODE && resultCode ==101){
=======
        if (requestCode == LocalCityFragment.REQUESTCODE && resultCode == 101) {
>>>>>>> NewsTop
            LocalCityFragment localCityFragment = (LocalCityFragment) FragmentFactory.getFragment("news_local");
            localCityFragment.getCityName(data.getStringExtra("city"));
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_Chnanel:
                if (isShowChnanel) {
                    showAnimation();
                    showChnanel();
                    showTranslateAnimation();
                } else {
                    hideAnimation();
                    saveNewsTitles();
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
                   hideTranslateAnimation();
=======
                    hideTranslateAnimation();
>>>>>>> NewsTop
                }
                isShowChnanel = !isShowChnanel;
                fragmentPagerAdapter.notifyDataSetChanged();
                viewPageIndicator.setViewPage(lazyViewPager, getSlidingMenu());
                break;
            //设置编辑按钮
            case R.id.myCompile_text:
                if (complete) {
                    myCompile_text.setText("完成");
                    MyApplication.showChnanelLongListener = true;
                    complete = false;
                } else {
                    myCompile_text.setText("编辑");
                    MyApplication.showChnanelLongListener = false;
                    saveNewsTitles();
                    complete = true;
                }

                myChnanelAdapater.notifyDataSetChanged();
                break;
        }
    }


<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935

=======
>>>>>>> NewsTop
    //显示频道信息动画
    private void showAnimation() {
        RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        animation.setFillAfter(true);
        iv_Chnanel.startAnimation(animation);
    }

    //隐藏频道信息动画
    private void hideAnimation() {
        RotateAnimation animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        animation.setFillAfter(true);
        iv_Chnanel.startAnimation(animation);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.gv_my_Chnanel:
                if (MyApplication.showChnanelLongListener) {
                    if (position != 0) {
                        TitileUtils.moreChannel.add(TitileUtils.myChannel.get(position));
                        TitileUtils.myChannel.remove(position);
                        myChnanelAdapater.notifyDataSetChanged();
                        moreChnanelAdapater.notifyDataSetChanged();

                    }
                } else {
                    //清空之前数据，将新的数据写入文件中
                    saveNewsTitles();
                    hideChnanel();
                    isShowChnanel = true;
                    fragmentPagerAdapter.notifyDataSetChanged();
                    viewPageIndicator.setViewPage(lazyViewPager, getSlidingMenu());
                    //设置viewpage显示当前点击该页
                    lazyViewPager.setCurrentItem(position);
                    hideTranslateAnimation();
                }
                break;

            case R.id.gv_more_Chnanel:
                TitileUtils.myChannel.add(TitileUtils.moreChannel.get(position));
                TitileUtils.moreChannel.remove(position);
                myChnanelAdapater.notifyDataSetChanged();
                moreChnanelAdapater.notifyDataSetChanged();
                fragmentPagerAdapter.notifyDataSetChanged();
                viewPageIndicator.setViewPage(lazyViewPager, getSlidingMenu());
                break;
        }

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        MyApplication.showChnanelLongListener = true;
        if (myCompile_text.getText().toString().equals("编辑")) {
            myCompile_text.setText("完成");
            complete = false;
        }


        myChnanelAdapater.notifyDataSetChanged();
        return true;
    }

    public void showChnanel() {
        myChnanel_relativeLayout.setVisibility(View.VISIBLE);
        scrollView.setVisibility(View.VISIBLE);
        viewPageIndicator.setVisibility(View.GONE);
        lazyViewPager.setVisibility(View.GONE);
    }

    public void hideChnanel() {
        myChnanel_relativeLayout.setVisibility(View.GONE);
        scrollView.setVisibility(View.GONE);
        viewPageIndicator.setVisibility(View.VISIBLE);
        lazyViewPager.setVisibility(View.VISIBLE);

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
}


    //平移动画
    public void hideTranslateAnimation(){
        TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,
                Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,-1);
=======
    }


    //平移动画
    public void hideTranslateAnimation() {
        TranslateAnimation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1);
>>>>>>> NewsTop
        animation.setDuration(500);
        scroll_LinearLayout.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
               hideChnanel();
=======
                hideChnanel();
>>>>>>> NewsTop
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

//        AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//        alphaAnimation.setDuration(500);
//        myChnanel_relativeLayout.startAnimation(alphaAnimation);
    }

    private void showTranslateAnimation() {
        TranslateAnimation animation = new TranslateAnimation(
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
                Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,
                Animation.RELATIVE_TO_PARENT,-1,Animation.RELATIVE_TO_PARENT,0);
=======
                Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0);
>>>>>>> NewsTop
        animation.setDuration(500);
        scroll_LinearLayout.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

//        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
//        alphaAnimation.setDuration(500);
//        alphaAnimation.setFillAfter(true);
//        mtChnanel_relativeLayout.startAnimation(alphaAnimation);
    }

    //保存频道改变后的新数据
    public void saveNewsTitles() {

        //将之前保存的数据移除
        CommonUtils.removeSp("myChannel");
        CommonUtils.removeSp("moreChannel");
        //将新数据保存进去
        StringBuilder stringBuilder1 = new StringBuilder();
        for (int i = 0; i < TitileUtils.myChannel.size(); i++) {
            stringBuilder1.append(TitileUtils.myChannel.get(i) + ",");
        }
        CommonUtils.saveSp("myChannel", stringBuilder1.toString());

        StringBuilder stringBuilder2 = new StringBuilder();
        for (int i = 0; i < TitileUtils.moreChannel.size(); i++) {
            stringBuilder2.append(TitileUtils.moreChannel.get(i) + ",");
        }
        CommonUtils.saveSp("moreChannel", stringBuilder2.toString());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
      /*  TitileUtils.myChannel.clear();
        TitileUtils.moreChannel.clear();*/
    }
}


